﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using MvcApp.CommonSupport.Filter;
using DoNet.Common.Helper;
using DoNet.FrameWork.Repository.Specification;
using Authority.Entity;
using Authority.Services;
using MvcApp.Models;
using MvcApp.CommonSupport;

namespace MVC.Controllers
{
    public class OrganizeController : Controller
    {
        private readonly IOrganizeService _organizeService;

        public OrganizeController(IOrganizeService organizeService)
        {
            _organizeService = organizeService;
        }

        [AuthorizeFilter(ModuleName = "001001", PermisionType = 1)]
        public ActionResult Organize()
        {
            return View();
        }

        public JsonResult OrganizeJsonList()
        {
            ISpecification<Organize> condition = new DirectSpecification<Organize>(o => o.IsDeleted == false);
            Expression<Func<Organize, string>> orderByExpression = o => o.Code;
            bool IsDESC = false;
            List<Organize> organizes = _organizeService.GetList<string>(condition, orderByExpression, IsDESC);
            //转换为jquery easyui 格式的 json
            List<Dictionary<string, object>> organizeTree = new List<Dictionary<string, object>>();
            organizes.ForEach(o =>
            {
                Dictionary<string, object> item = new Dictionary<string, object>();
                item.Add("Id", o.Id);
                item.Add("Code", o.Code);
                item.Add("OrganizeName", o.OrganizeName);
                if (o.ParentId != new Guid("00000000-0000-0000-0000-000000000000"))
                {
                    item.Add("_parentId", o.ParentId);
                    //item.Add("state", "closed");
                }
                else
                {
                    item.Add("state", "open");
                }
                organizeTree.Add(item);
            });

            TreeGridData data = new TreeGridData
            {
                total = organizeTree.Count,
                rows = organizeTree
            };
            return Json(data);
        }

        #region 另一种 treeGrid 数据
        //            return Content(@"[{
//	""Id"":1,
//	""Code"":""01"",
//	""OrganzeName"":""name1"",
//	""children"":[{
//		""Id"":2,
//		""Code"":""0101"",
//		""OrganzeName"":""name11""
//	},{
//		""Id"":3,
//		""Code"":""0102"",
//		""OrganzeName"":""name12""
//	}]
//}]");

            //return Json(
            //    new List<TreeData>
            //    {
            //        new TreeData{
            //            Id = Guid.NewGuid(),
            //            Code = "001001",
            //            OrganizeName = "Folder1",
            //            children = new List<TreeData> {
            //                new TreeData{
            //                    Id = Guid.NewGuid(),
            //                    Code = "001001",
            //                    OrganizeName = "Folder2"
            //                }
            //            }
            //        }
            //    });


        //class TreeData
        //{
        //    public Guid Id { get; set; }
        //    public string Code { get; set; }
        //    public string OrganizeName { get; set; }
        //    public List<TreeData> children { get; set; }
        //}
        #endregion

        [AuthorizeFilter(ModuleName = "001001", PermisionType = 2)]
        public ActionResult InsertOrEditOrganize(Organize organize)
        {
            CurrentUser currentUser = new CurrentUser();
            organize.SysCreatedBy = currentUser.UserInfo.Id;
            organize.SysCreatedOn = DateTime.Now;
            organize.SysModifiedBy = currentUser.UserInfo.Id;
            organize.SysModifiedOn = DateTime.Now;
            string rtn = _organizeService.InsertOrEditOrganize(organize);
            return Content(rtn);
        }

        [AuthorizeFilter(ModuleName = "001001", PermisionType = 4)]
        public ActionResult DeleteOrganizeById(string id)
        {
            _organizeService.DeleteOrganizeById(new Guid(id));
            return Content("删除成功！");
        }

        public JsonResult GetOrganizeTree()
        {
            List<TreeDataOfChildren> tds = new TreeDataOfChildren().Get(_organizeService,
                                                        new Guid("00000000-0000-0000-0000-000000000000"));
            return Json(tds);
        }

        public ActionResult GetOrganizeNameById(string id)
        {
            ISpecification<Organize> condition = new DirectSpecification<Organize>(o => o.Id == new Guid(id));
            Organize org =  _organizeService.GetByCondition(condition);
            return Content(org.OrganizeName);
        }

    }
}
